package com.exper;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class myCharFilter implements Filter {

    private String encode = "utf-8";

    public void init(FilterConfig filterConfig) throws ServletException {
        if (filterConfig.getInitParameter("encode") != null) {
            encode = filterConfig.getInitParameter("encode");
        }
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse rep = (HttpServletResponse) response;
        rep.setCharacterEncoding(encode);
        rep.setContentType("text/html;charset=utf-8");
        if ("POST".equals(req.getMethod())) {
            req.setCharacterEncoding(encode);

        }

        chain.doFilter(req, rep);
    }

    public void destroy() {

    }
}
